﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using FineUI;
using SDLITS.Model;
using SDLITS.Common;
using MySoft.Data;

namespace SDLITS.Web.Customer
{
    public partial class ContractManage : CommonBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindCom();
                BindGrid(GetWhere());
            }
        }
        private void BindGrid(WhereClip wc)
        {
            Grid1.RecordCount = SDDbSession.Default.From<V_ContractCompany>().Where(wc).Count();
            List<V_ContractCompany> items = SDDbSession.Default.From<V_ContractCompany>().Where(wc).Page(Grid1.PageSize, (Grid1.PageIndex + 1)).ToList();
            Grid1.DataSource = items;
            Grid1.DataBind();
        }
        protected void BindCom()
        {
            List<Company> coms = SDDbSession.Default.From<Company>().Where(Company._.StatusID == 1).ToList();
            drp_com.DataSource = coms;
            drp_com.DataTextField = "CompanyName";
            drp_com.DataValueField = "CompanyID";
            drp_com.DataBind();
            drp_com.Items.Insert(0, new ListItem("--全部公司--", "-1"));
        }
        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid(GetWhere());
        }
        protected WhereClip GetWhere()
        {
            WhereClip wc = WhereClip.All;
            if (drp_com.SelectedValue!="-1")
            {
                wc = wc && V_ContractCompany._.CompanyID==drp_com.SelectedValue;
            }
            return wc;
        }
        
        protected void Grid1_RowDataBound(object sender, GridRowEventArgs e)
        {
            e.Values[3] = Methed.Condition(e.Values[3].ToString());
        }
        protected void btn_xq_Click(object sender, EventArgs e)
        {
            if (drp_com.SelectedValue == "-1")
            {
                Alert.Show("请先选择需要续签的公司");
                return;
            }
            PageContext.RegisterStartupScript(Window2.GetShowReference("ContractChange.aspx?id=" + drp_com.SelectedValue, "续签合同-" + drp_com.SelectedText));
        }
        protected void btn_zz_Click(object sender, EventArgs e)
        {
            if (drp_com.SelectedValue == "-1")
            {
                Alert.Show("请先选择需要终止的公司");
                return;
            }
            if (SDDbSession.Default.From<Contract>().Where(Contract._.CompanyID == drp_com.SelectedValue && Contract._.StatusID == 1).Count() > 0)
            {
                Contract con = SDDbSession.Default.From<Contract>().Where(Contract._.CompanyID == drp_com.SelectedValue && Contract._.StatusID == 1).OrderBy(Contract._.CreateDate.Desc).ToFirst();
                lbl_no.Text = con.ContractNo;
                lbl_start.Text = con.StartDate.ToString();
            }
            else
            {
                Contract con = SDDbSession.Default.From<Contract>().Where(Contract._.CompanyID == drp_com.SelectedValue).OrderBy(Contract._.CreateDate.Desc).ToFirst();
                lbl_no.Text = con.ContractNo;
                lbl_start.Text = con.StartDate.ToString();
            }
            hidid.Text = drp_com.SelectedValue;
            lbl_name.Text = drp_com.SelectedText;
            PageContext.RegisterStartupScript(Window3.GetShowReference());
        }
        protected void drp_com_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindGrid(GetWhere());
        }
        protected void Window2_Close(object sender, WindowCloseEventArgs e)
        {
            //BindGrid(GetWhere());
        }
        protected void btnSaveRefresh_Click(object sender, EventArgs e)
        {
            string cid = hidid.Text;
            string start = lbl_start.Text;
            string end = txt_enddate.Text;
            int ExpenseYear = Convert.ToDateTime(end).Year;
            int ExpenseMonth = Convert.ToDateTime(end).Month;
            int result = 0;
            if (SDDbSession.Default.From<Contract>().Where(Contract._.CompanyID == cid && Contract._.StatusID == 1 && Contract._.RentActual.IsNull() && Contract._.IsFeeDone == false).Count() > 0)
            {
                Alert.Show("请先缴清相关电费和房费");
                return;
            }
            result += SDDbSession.Default.FromSql("Update [Room] Set StatusID=1 Where RoomID=(select top 1 ct.RoomID from [Contract] ct where ct.CompanyID=" + cid + " order by ct.CreateDate Desc)").ExecuteNonQuery();
            result += SDDbSession.Default.FromSql("Update [Company] Set StatusID=2,RoomID=NULL Where CompanyID=" + cid).ExecuteNonQuery();
            result += SDDbSession.Default.FromSql("Update [User] Set StatusID=2 Where CompanyID=" + cid).ExecuteNonQuery();
            result += SDDbSession.Default.FromSql("Update [Contract] Set StatusID=2 Where CompanyID=" + cid).ExecuteNonQuery();
            result += SDDbSession.Default.FromSql("Update [Contract] Set EndDate='" + end + "' Where ContractID=(select top 1 ct.ContractID from [Contract] ct where ct.CompanyID=" + cid + " order by ct.CreateDate Desc)").ExecuteNonQuery();
            result += SDDbSession.Default.FromSql("UPDATE [MemCard] SET [StartDate] = '" + start + "', [ExpireDate] = '" + end + "' WHERE [CompanyID] = " + cid).ExecuteNonQuery();
            result += SDDbSession.Default.FromSql("Delete  FROM [Expense]where CompanyID=" + cid + " and (ExpenseYear>" + ExpenseYear + " or (ExpenseYear=" + ExpenseYear + " and ExpenseMonth>" + ExpenseMonth + ")) ").ExecuteNonQuery();
            if (result > 0)
            {
                PageContext.RegisterStartupScript(Window3.GetHideRefreshReference());
            }
        }
    }
}
